package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.GameSettings;
import ru.compscicenter.uno.log.Logger;
import ru.compscicenter.uno.model.Rules;
import ru.compscicenter.uno.util.ImagePool;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

/**
 * User: SLeeP
 * Date: 10.03.12
 * Time: 0:52
 */
public class Panel3ServerWait extends JPanel {

    private JLabel blankSpace;
    private JLabel blankSpace2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel waitingForPlayers;
    private long startTime;

    private JLabel welcomeTitle;
    private JPanel contentPanel;
    private JLabel myIP;

    public static JTextField myIPtext;
    private JLabel iconLabel;
    private ImageIcon icon;

    private static DefaultListModel<Object> listModel;
    private static JList<Object> connectedPlayersList;
    GameSettings gameSettings;

    public Panel3ServerWait(GameSettings gameSettings) {
        this.gameSettings = gameSettings;
        iconLabel = new JLabel();
        contentPanel = getContentPanel();
        contentPanel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

        icon = new ImageIcon(ImagePool.getImage(Wizard.unoIconDIR, 250, 383));

        setLayout(new java.awt.BorderLayout());

        if (icon != null)
            iconLabel.setIcon(icon);

        iconLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED));

        add(iconLabel, BorderLayout.WEST);

        JPanel secondaryPanel = new JPanel();
        secondaryPanel.add(contentPanel, BorderLayout.NORTH);
        add(secondaryPanel, BorderLayout.CENTER);
    }

    private JPanel getContentPanel() {
        JPanel contentPanel1 = new JPanel();
        JPanel jPanel1 = new JPanel();

        welcomeTitle = new JLabel();
        welcomeTitle.setAlignmentX(LEFT_ALIGNMENT);
        blankSpace = new JLabel();
        blankSpace2 = new JLabel();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        myIP = new JLabel("This server's IP: ");
        myIP.setAlignmentX(LEFT_ALIGNMENT);
        myIPtext = new JTextField();
        myIPtext.setEditable(false);
        listModel = new DefaultListModel<Object>();

        connectedPlayersList = new JList<Object>(listModel);
        connectedPlayersList.setAlignmentX(LEFT_ALIGNMENT);
        connectedPlayersList.setPreferredSize(new Dimension(250, 150));
        connectedPlayersList.setMinimumSize(new Dimension(250, 150));
        connectedPlayersList.setMaximumSize(new Dimension(250, 150));

        welcomeTitle.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 14));
        welcomeTitle.setText("Connected players:");
        jPanel1.add(welcomeTitle);
        
        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.PAGE_AXIS));

        jPanel1.add(connectedPlayersList);
        myIP.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 14));
        jPanel1.add(myIP);
        jPanel1.add(myIPtext);
        contentPanel1.add(jPanel1, java.awt.BorderLayout.CENTER);
        updatePlayersList();
        return contentPanel1;
    }


    public void updatePlayersList() {
        Thread upd = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    if (gameSettings.serverController != null) {
                        if (gameSettings.serverController.isReady) {
                            break;
                        }
                        ArrayList<String> names = gameSettings.serverController.getConnectedPlayersNames();
                        gameSettings.serverController.sendAllPlayersNames();
                        listModel.clear();
                        //listModel.addElement(gameSettings.serverController.getPlayer(gameSettings.serverController.getCurrentPlayerId()).getName());
                        for (String name: names) {
                            listModel.addElement(name);
                        }
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        });
        upd.setDaemon(true);
        upd.start();
    }
}
